import oss2
import fc2
import os
import json
import uuid

class AliyunService:
    def __init__(self):
        self.auth = oss2.Auth(
            os.getenv('ALIYUN_ACCESS_KEY'),
            os.getenv('ALIYUN_ACCESS_SECRET')
        )
        self.bucket = oss2.Bucket(
            self.auth,
            os.getenv('ALIYUN_OSS_ENDPOINT'),
            os.getenv('ALIYUN_BUCKET_NAME')
        )
        self.fc_client = fc2.Client(
            endpoint=os.getenv('ALIYUN_FC_ENDPOINT'),
            accessKeyID=os.getenv('ALIYUN_ACCESS_KEY'),
            accessKeySecret=os.getenv('ALIYUN_ACCESS_SECRET')
        )
    
    def generate_emoji(self, image_data, style):
        # 上传原图到OSS
        image_key = f"inputs/{uuid.uuid4()}.png"
        self.bucket.put_object(image_key, image_data)
        
        # 调用函数计算进行处理
        response = self.fc_client.invoke_function(
            'emoji-generator',
            'process-image',
            payload=json.dumps({
                'image_key': image_key,
                'style': style
            })
        )
        return response.data

# 在 emoji_controller.py 中使用
@ns.route('/generate')
class EmojiGenerator(Resource):
    def post(self):
        try:
            # ... 验证代码 ...
            
            # 使用阿里云服务生成
            aliyun_service = AliyunService()
            result = aliyun_service.generate_emoji(
                image_data=request.files['image'].read(),
                style=data['style']
            )
            
            # ... 保存结果代码 ... 